
package service

import (
	"context"
	"log"
	"guli-shopping/product-service/dao"
	"guli-shopping/product-service/protobuff/productProto"
	"strconv"
	"strings"
)

type PmsCategoryBrandRelationService struct {
}


func getPmsCategoryBrandRelationResponse() *productProto.PmsCategoryBrandRelationResponse {
	return &productProto.PmsCategoryBrandRelationResponse{}
}


func (this PmsCategoryBrandRelationService) FindAllByBrandId(ctx context.Context, request *productProto.PmsCategoryBrandRelationRequest) (*productProto.PmsCategoryBrandRelationResponse, error) {
	response := getPmsCategoryBrandRelationResponse()
	if request==nil || request.Id==0 {
		response.Code=403
		response.Message="缺少id"
		return response,nil
	}
	all, err := dao.PmsCategoryBrandRelationDao.FindAllByBrandId(request.Id)
	if err!=nil {
		response.Code=500
		response.Message=err.Error()
		//记录错误
		log.Println(err)
		return response,nil
	}
	response.List=all
	response.Code=200
	response.Message="查询成功"
	return response,nil

}

func (this* PmsCategoryBrandRelationService) FindAll(ctx context.Context, request *productProto.PmsCategoryBrandRelationRequest) (*productProto.PmsCategoryBrandRelationResponse, error) {
	response := getPmsCategoryBrandRelationResponse()

    if request.Query==nil {
      request.Query= &productProto.PmsCategoryBrandRelationQuery{Model: &productProto.PmsCategoryBrandRelation{}}
    }
    if request.Query.Page<=0{
        request.Query.Page=1
    }
    if request.Query.Size<=0 {
        request.Query.Size=5
    }

	all, count,err := dao.PmsCategoryBrandRelationDao.FindAll(request.Query)
	if err!=nil {
		response.Code=500
		response.Message=err.Error()
		//记录错误
		log.Println(err)
		return response,nil
	}
	response.Total=int64(count)
	response.List=all
	response.Code=200
	response.Message="查询成功"
	return response,nil
}

func (this* PmsCategoryBrandRelationService) FindById(ctx context.Context, request *productProto.PmsCategoryBrandRelationRequest) (*productProto.PmsCategoryBrandRelationResponse, error) {
	response := getPmsCategoryBrandRelationResponse()
	if request==nil || request.Id==0 {
		response.Code=403
		response.Message="缺少id"
		return response,nil
	}
	all, err := dao.PmsCategoryBrandRelationDao.FindById(request.Id)
	if err!=nil {
		response.Code=500
		response.Message=err.Error()
		//记录错误
		log.Println(err)
		return response,nil
	}
	response.Model=all
	response.Code=200
	response.Message="查询成功"
	return response,nil
}

func (this* PmsCategoryBrandRelationService) Delete(ctx context.Context, request *productProto.PmsCategoryBrandRelationRequest) (*productProto.PmsCategoryBrandRelationResponse, error) {
	response := getPmsCategoryBrandRelationResponse()
	if request==nil || request.Ids==""{
		response.Code=403
		response.Message="需要指定id"
		return response,nil
	}
	//转换id
	ints:=[]int64{}
	split := strings.Split(request.Ids, ",")
	for _, value := range split {
		parseInt, _ := strconv.ParseInt(value, 10, 64)
		ints=append(ints,parseInt)
	}
	err := dao.PmsCategoryBrandRelationDao.Delete(ints)
	if err!=nil {
		response.Code=500
		response.Message=err.Error()
		//记录错误
		log.Println(err)
		return response,nil
	}
	response.Code=200
	response.Message="删除成功"
	return response,nil
}

func (this* PmsCategoryBrandRelationService) Update(ctx context.Context, request *productProto.PmsCategoryBrandRelationRequest) (*productProto.PmsCategoryBrandRelationResponse, error) {
	response := getPmsCategoryBrandRelationResponse()
	if request==nil || request.Model==nil || request.Model.Id==0{
		response.Code=403
		response.Message="需要指定对象和id"
		return response,nil
	}
	 err := dao.PmsCategoryBrandRelationDao.Update(request.Model)
	if err!=nil {
		response.Code=500
		response.Message=err.Error()
		//记录错误
		log.Println(err)
		return response,nil
	}
	response.Code=200
	response.Message="更新成功"
	return response,nil
}

func (this* PmsCategoryBrandRelationService) Save(ctx context.Context, request *productProto.PmsCategoryBrandRelationRequest) (*productProto.PmsCategoryBrandRelationResponse, error) {
	response := getPmsCategoryBrandRelationResponse()
	if request==nil || len(request.List)==0{
		response.Code=403
		response.Message="数组错误"
		return response,nil
	}
	err := dao.PmsCategoryBrandRelationDao.Save(request.List)
	if err!=nil {
		response.Code=500
		response.Message=err.Error()
		//记录错误
		log.Println(err)
		return response,nil
	}
	response.Code=200
	response.Message="保存成功"
	return response,nil
}












